import Banner from "./Banner";
import { DataManager } from "./Framework/Managers/DataManager";
import { UIManager } from "./Framework/Managers/UIManager";
import { ZTool } from "./Framework/Utils/ZTool";


const { ccclass, property } = cc._decorator;

@ccclass
export default class TreasureBox extends cc.Component {
    @property(cc.Sprite)
    jdt: cc.Sprite = null;//进度条
    index: number = 0;

    public static ShowTreasureBox() {
        cc.resources.load("Prefabs/宝箱", cc.Prefab, (err, date) => {
            if (err) {
                console.log("没有找到宝箱预制体");
                return;
            }
            console.log("生成宝箱");
            let pre = cc.instantiate(date);
            pre.setParent(cc.find("Canvas"));
            pre.setSiblingIndex(99999);
            pre.setPosition(0, 0, 0);
        })

    }

    gived: boolean = false;

    Show() {
        this.gived = false;
        cc.tween(this.node.getChildByName("其他").getChildByName("光效"))
            .to(2, { angle: 360 })
            .to(0, { angle: 0 })
            .union()
            .repeatForever()
            .start();
        this.schedule(() => {
            if (this.index > 0) this.index -= 1;
        }, 0.1)
    }

    //点击宝箱按钮
    OnButtonClick() {
        this.index += 5;
        if (this.index >= 60) {
            Banner.Instance.ShowVideoAd(() => { this.GiveReward(); });
            this.scheduleOnce(() => {
                this.node.destroy();
            }, 2);
        }
    }

    protected update(dt: number): void {
        this.jdt.fillRange = this.index / 100;
    }

    //提供奖励且销毁
    GiveReward() {
        if (this.gived) return;
        this.gived = true;
        let money = ZTool.GetRandomInt(500, 1000);
        UIManager.ShowTip(`获得金钱${money}`);
        DataManager.Money += money;
    }
}
